package com.loie.xcx.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.*;

public class SOAPClient {

    public static void main(String[] args) {
        try {
            // Replace with your SOAP web service URL
            String url = "http://ws.webxml.com.cn/WebServices/ValidateCodeWebService.asmx/cnValidateImage";

            // Encode the byString parameter
            String byString = "12345678";
            String encodedByString = URLEncoder.encode(byString, "UTF-8");

            // Create the URL-encoded data
            String urlParameters = "byString=" + encodedByString;

            // Create a URL object
            URL obj = new URL(url);

            // Open a connection to the URL
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

            // Set the necessary HTTP method and headers
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // Enable input/output streams and set the URL-encoded data
            connection.setDoOutput(true);
            OutputStream os = connection.getOutputStream();
            os.write(urlParameters.getBytes());
            os.flush();
            os.close();

            // Get the HTTP response code
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // Read the response as binary data
            InputStream inputStream = connection.getInputStream();

            // Specify the path where you want to save the image
            Path imagePath = Paths.get("D:\\wsdlcode\\1.jpg");

            // Save the binary data to the image file
            Files.copy(inputStream, imagePath, StandardCopyOption.REPLACE_EXISTING);

            // Close the input stream
            inputStream.close();

            System.out.println("Image saved successfully.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}